<?php

require("../head.php");

if (!niveauOK(1)) return ;

$requete = "SELECT Eleve.*, COUNT(Rush.id) AS nr, MAX(Rush.qualite) AS mq FROM Eleve LEFT JOIN Rush ON Rush.eleve = Eleve.id WHERE 1=1" ;

// Requete de recherche des noms
$nom = $_POST['nom'] ;
$section = $_POST['section'] ;
//echo $nom ;
$nom = preg_replace ("/\s+/", " ", $nom);

$noms = explode(" ", $nom) ;

if (!empty($nom))
{
//   $requete .= " AND (" ;
   $premier = true ;
   foreach ($noms as $value)
   {
      if (empty($value)) continue ;
      if ($premier)
      {
         $requete .= " AND (" ;
         $premier = false ;
      }
      else
         $requete .= " OR" ;
      $requete .= " CONCAT(nom, CONCAT(' ', prenom)) LIKE \"%".$value."%\"" ;
      //echo $value."<br/>".PHP_EOL ;
   }
   if (!$premier) $requete .= ")";
}

// Section
if (!empty($section))
{
   $requete .= " AND section=\"{$section}\"" ;
}

// Groupement
$requete .= " GROUP BY Eleve.id ORDER BY section, nom" ;
//$requete .= " GROUP BY Eleve.id ORDER BY nom, prenom" ;

// Fin de la construction de la requète
$requete .= ";" ;

// exécution de la requète
connect() ;
$result = executerRequete($requete, $GLOBALS['debug']) ;

// Affichage
?>

<table class="listEleves liste" style="margin-left:auto;margin-right:auto;">
   <tr>
      <th colspan="4">Légende</th>
   </tr>
   <tr>
      <td colspan="4">Qualit&eacute; du meilleur rush :</th>
   </tr>
   <tr class="eleveTrombi2">
      <td colspan="4" style="text-align:center;">Bon</td>
   </tr>
   <tr class="eleveTrombi1">
      <td colspan="4" style="text-align:center;">Moyen</td>
   </tr>
   <tr class="eleveTrombi0">
      <td colspan="4" style="text-align:center;">Mauvais</td>
   </tr>
   <tr>
      <th style="width:10px">Timeline</th>
      <th style="width:300px">Nom</th>
      <th style="width:150px">Section</th>
      <th style="width:150px">Nombre de rush</th>
   </tr>
<?php
$nombreChoisi = 0 ;
while ($row = mysql_fetch_assoc($result))
{
   $choisi = 0 ;
   if ($row['rush']>0)
   {
      $nombreChoisi++ ;
      $choisi = 1 ;
   }
?>
<tr class="eleveTrombi<?= $row['mq'] ?> eleveTrombiChoisi<?= $choisi ?>">
   <td class="timeline timeline<?= $row['id'] ?>" style="text-align:center">
<input type="checkbox" name="timeline_<?= $row['id'] ?>" <?php
if ($row['timeline'] > 0) { ?>checked<?php } ?>/>
<script language="Javascript">
$("input[name='timeline_<?= $row['id'] ?>']").click(function(){
   var checked=0 ;
   if ($(this).attr("checked"))
   { checked = 1 ;}
   $.post("ajax/changeTimeline.php", {id:<?=$row['id'] ?>, t:checked}) ;
}) ;
</script>
</td>
   <td class="nom"><a href="index.php?page=eleve&id=<?= $row['id'] ?>"><?= $row['prenom']." ".$row['nom'] ?></a></td>
   <td class="section" style="text-align:center;"><?= $row['section'] ?></td>
   <td class="nombreRush" style="text-align:center;"><?= $row['nr'] ?></td>
</tr>
<?php } ?>
</table>

<?php

if (niveauOK(3)) echo $requete ;
echo "<br/><b>".$nombreChoisi." Rushs choisis</b><br/>".PHP_EOL ;

mysql_close() ;

?>
